home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Misc / NewFile / Source / Main.m < prev    next >
Text File  |  1993-06-24  |  5KB  |  212 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "Main.h"
  5. #import "Protos.h"
  6. #import "Server.h"
  7. #import <appkit/Application.h>
  8. #import <appkit/Listener.h>
  9. #import <appkit/Speaker.h>
  10. #import <appkit/Panel.h>
  11. #import <objc/List.h>
  12. #import <stdio.h>
  13. #import <libc.h>
  14. #import <defaults/defaults.h>
  15.  
  16. // Temporary files for updating services.
  17. #define SERVICE_FILE "/tmp/NewFileService"
  18. #define EXEC_FILE "/tmp/NewFileExec"
  19.  
  20. @implementation Main
  21.  
  22. // The application's folder.
  23. - (char *)appdir
  24. {
  25.     return appdir;
  26. }
  27.  
  28. // Translate a prototype's name into an absolute pathname stored in fullname.
  29. // Returns fullname.
  30. - (char *)fullprotoname:(char *)fullname:(char *)name
  31. {
  32.     sprintf(fullname, "%s/blanks/%s", [self appdir], name);
  33.     return fullname;
  34. }
  35.  
  36. // Copy a file/directory.
  37. - (int)copyfile:(char *)to:(char *)from
  38. {
  39.     int reply;
  40.     char command[3*MAXPATHLEN];
  41.     sprintf(command,"/bin/cp -rp '%s' '%s'", from, to);
  42.     reply = system(command);
  43.     return reply;;
  44. }
  45.  
  46. - appDidInit:sender
  47. {
  48.     //Find the application's folder.
  49.     strcpy(appdir, NXArgv[0]);
  50.     *rindex(appdir, '/') = '\0';
  51.  
  52.     listener = [NXApp appListener];
  53.     speaker = [NXApp appSpeaker];
  54.     [listener setServicesDelegate:server];
  55.     
  56.     // Read in list of types.
  57.     [self loadstate];
  58.     // Tell the servier to initialize itself.
  59.     [server inittypes:[protos protoslist]];
  60.     
  61.     return self;
  62. }
  63.  
  64. /* Intercept termination to write text and defaults.*/
  65. - appWillTerminate:sender
  66. {
  67.     [self savestate];
  68.     return self;
  69. }
  70.  
  71.  
  72. // Initialization proper.
  73. - loadstate
  74. {
  75.     char    rc[MAXPATHLEN];
  76.     FILE *fp;
  77.     
  78.     // First, check for rc file in the user's home directory.
  79.     sprintf(rc, "%s/.NewFilerc", getenv("HOME"));
  80.     fp = fopen(rc, "r");
  81.     if (fp)
  82.     {
  83.         [protos readprotos:fp];
  84.         fclose(fp);
  85.     }
  86.     else
  87.     {
  88.         // No rc file in home directory, so use the one in the app's folder,
  89.         sprintf(rc, "%s/NewFilerc", [self appdir]);
  90.         fp = fopen(rc, "r");
  91.         if (fp)
  92.         {
  93.             [protos readprotos:fp];
  94.             fclose(fp);
  95.         }
  96.         else
  97.             // Should never get to here.
  98.             fprintf(stderr, "NewFile: can't open rc file: your version of NewFile is incomplete.\n");
  99.     }
  100.     
  101.     return self;
  102. }
  103.  
  104. // Store the defaults.
  105. - savestate;
  106. {
  107.     char    rc[MAXPATHLEN];
  108.     FILE *fp;
  109.     sprintf(rc, "%s/.NewFilerc", getenv("HOME"));
  110.     fp = fopen(rc, "w");
  111.     if (fp)
  112.     {
  113.         [protos writeprotos:fp];
  114.         fclose(fp);
  115.     }
  116.     else
  117.         fprintf(stderr, "NewFile: can't open rc file\n");
  118.     return self;
  119. }
  120.  
  121.  
  122. // Open a file by messaging the Workspace Manager.
  123. - openinws:(char *)file
  124. {
  125.     int msgDelivered, fileOpened;
  126.     port_t    wsport;
  127.  
  128.     wsport = NXPortFromName(NX_WORKSPACEREQUEST, NULL);
  129.     if (wsport == PORT_NULL) fprintf(stderr, "NewFile: Can't get Workspace port\n");
  130.     [speaker setSendPort:wsport];
  131.     msgDelivered = [speaker openFile:file ok:&fileOpened];
  132.     if ((msgDelivered!=0) || (fileOpened==NO))
  133.         fprintf(stderr, "NewFile: Couldn't open %s\n", file);
  134.     return self;
  135. }
  136.  
  137. // Open a file by messaging an application.
  138. - openineditor:(char *)file:(char *)app
  139. {
  140.     int msgDelivered, fileOpened; 
  141.     port_t port;
  142.     
  143.     port = NXPortFromName(app, NULL);
  144.     if (port!=PORT_NULL)
  145.     {
  146.         [speaker setSendPort:port];
  147.         msgDelivered = [speaker openFile:file ok:&fileOpened];
  148.         if ((msgDelivered!=0) || (fileOpened==NO))
  149.             fprintf(stderr, "NewFile: Couldn't open %s\n", file);
  150.     }
  151.     else
  152.     {
  153.         fprintf(stderr, "NewFile: Couldn't get port for %s. Opening in Workspace\n", app);
  154.         [self openinws:file];
  155.     }
  156.     return self;
  157. }
  158.  
  159. // I'd rather discuss this.
  160. - updateservices:sender
  161. {
  162.     char cmd[MAXPATHLEN];
  163.     FILE *serviceFile;
  164.     int i, count;
  165.     id    list;
  166.     char    *typename;
  167.     
  168.     if (NXRunAlertPanel("Update services?", "This action will alter the executable file and exit NewFile.", "Go ahead", "Cancel", NULL) == NX_ALERTDEFAULT)
  169.     {
  170.         [self savestate];
  171.         if ((serviceFile = fopen(SERVICE_FILE, "w")) == NULL) {
  172.             NXRunAlertPanel(NULL, "Could not open temporary file %s", NULL, NULL, 
  173.                 NULL, SERVICE_FILE);
  174.             exit(1);
  175.         }
  176.         
  177.         list = [protos protoslist];
  178.         count = [list count];
  179.         for (i=0;i<count;i++)
  180.         {
  181.             typename = [[list objectAt:i] typename];
  182.             fprintf(serviceFile, "Message: createNew\nPort: NewFile\nMenu Item: New File/%s\nUser Data: %s\nSend Type: NXFilenamePboardType\nDeactivate Requestor: YES\n\n", typename, typename);
  183.         }
  184.         fclose(serviceFile);
  185.     
  186.         /*
  187.             * OK, now we have a new service file, so insert it in the executable.
  188.             */
  189.             sprintf(cmd, "/bin/segedit %s -r __ICON __services %s -o %s", NXArgv[0],
  190.             SERVICE_FILE, EXEC_FILE);
  191.         if (system(cmd) != 0) {
  192.             NXRunAlertPanel(NULL, "Command %s failed!\nYou should copy NewFile.app into ~/Apps. See Help.", NULL, NULL, NULL, cmd);
  193.             exit(1);
  194.         }
  195.         sprintf(cmd, "/bin/rm -f %s ; /bin/mv %s %s ; chmod a+x %s", NXArgv[0], EXEC_FILE, 
  196.         NXArgv[0], NXArgv[0]);
  197.         if (system(cmd) != 0) {
  198.             NXRunAlertPanel(NULL, "Command %s failed!\nYou should copy NewFile.app into ~/Apps. See Help.", NULL, NULL, NULL, cmd);
  199.             exit(1);
  200.         }
  201.         if (system("make_services") != 0) {
  202.             NXRunAlertPanel(NULL, "Command make_services failed!", NULL, NULL, NULL);
  203.             exit(1);
  204.         }
  205.         NXRunAlertPanel(NULL, "Update of services complete. Applications launched from now on should show new services.", NULL, NULL, NULL);
  206.         exit(0);
  207.     }
  208.     return self;    
  209. }
  210.  
  211. @end
  212.